package org.word.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

@Slf4j
public class FileUtils {
    /**
     * 读取文件内容
     * @param filePath
     * @return
     */
    public static String getFileStr(String filePath) {
        ClassPathResource classPathResource = new ClassPathResource(filePath);
        String json = null;
        try (InputStream inputStream = classPathResource.getInputStream()) {
            json = StreamUtils.copyToString(inputStream, Charset.forName("UTF-8"));

            json = preprocessJsonStr(json);
            return json;
        } catch (IOException e) {
            log.error("{}",e);
            throw new RuntimeException();
        }
    }

    public static String preprocessJsonStr(String json) {
        json = json.replaceAll("\\$ref", "ref");
        json = json.replaceAll("\"200\"", "\"ret200\"");
        return json;
    }

}
